package com.newedu.equipmgr.servlet.system.user;

import com.alibaba.fastjson.JSON;
import com.newedu.equipmgr.entity.SysUser;
import com.newedu.equipmgr.entity.ReturnData;
import com.newedu.equipmgr.service.SysUserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


@WebServlet(name = "UserListServlet", urlPatterns = "/api/servlet/user/queryList")
public class UserListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);


        String queryName = req.getParameter("queryName");
        String pageNumber = req.getParameter("pageIndex");
        String offset = req.getParameter("offset");
        String pageSize = req.getParameter("pageSize");

        System.out.println("queryName:" + queryName);
        System.out.println("pageNumber:" + pageNumber);
        System.out.println("offset:" + offset);
        System.out.println("pageSize:" + pageSize);

        // 调用前端分页方法
//        frontPageing(resp);

        // 调用后端分页方法
        backPageing(queryName, pageNumber, offset, pageSize, resp);
    }

    private void backPageing(String queryName, String pageNumber, String offset, String pageSize, HttpServletResponse response) throws IOException {
        // 后端分页数据输出
        ReturnData<SysUser> returnData = new ReturnData<>();
        //设置为Json类型输出
        response.setContentType("application/json;charset=utf-8");

        // 简化数据库的操作，后面改为数据的操作

        List<SysUser> querylist = new ArrayList();
        int ioffset = Integer.parseInt(offset);
        int ipageNumber = Integer.parseInt(pageNumber);
        int ipageSize = Integer.parseInt(pageSize);
        // 人为的计算如何分页的
//        int end = (ioffset+ipageSize)>list.size()?(list.size()):(ioffset+ipageSize);
//        System.out.println("end:"+end);
//        for(int i= Integer.parseInt(offset);i<end;i++){
//            querylist.add(list.get(i));
//        }

        SysUserService areaService = new SysUserService();
        String condition = null;
        if(queryName!=null&& !queryName.isEmpty()){
            condition = " where username = '"+queryName+"'";
        }
        querylist = areaService.selectBy(condition,ipageNumber,ipageSize);

        //得到总页数
//        returnData.setTotal(12);
        int rows = areaService.countRows(condition);
        returnData.setTotal(rows);
        System.out.println(querylist);
        returnData.setRows(querylist);

        String result = JSON.toJSONString(returnData); // List转json
        System.out.println(result);
        PrintWriter out = response.getWriter();
        out.println(result);
        System.out.println(result);

        out.flush();
        out.close();

    }

//    private void frontPageing(HttpServletResponse response) throws IOException {
//        //        前端分页数据输出
//        //设置为Json类型输出
//        response.setContentType("application/json;charset=utf-8");
//        List<AreaInfo> list = new ArrayList();
//        list.add(new AreaInfo(1, "1号楼", "1层"));
//        list.add(new AreaInfo(2, "1号楼", "2层"));
//        list.add(new AreaInfo(3, "1号楼", "3层"));
//        list.add(new AreaInfo(4, "1号楼", "4层"));
//        list.add(new AreaInfo(5, "1号楼", "5层"));
//        list.add(new AreaInfo(6, "2号楼", "1层"));
//        list.add(new AreaInfo(7, "2号楼", "2层"));
//        list.add(new AreaInfo(8, "2号楼", "3层"));
//        list.add(new AreaInfo(9, "2号楼", "4层"));
//        list.add(new AreaInfo(10, "2号楼", "5层"));
//        list.add(new AreaInfo(11, "3号楼", "1层"));
//        list.add(new AreaInfo(12, "3号楼", "2层"));
//        list.add(new AreaInfo(13, "3号楼", "3层"));
//        String result = JSON.toJSONString(list); // List转json
//
//        PrintWriter out = response.getWriter();
//        out.println(result);
//        System.out.println(result);
//        out.flush();
//        out.close();
//    }
}

